//
//  AYuVisitorView.swift
//  SHWeibo3
//
//  Created by 余伟康 on 16/7/19.
//  Copyright © 2016年 wangce. All rights reserved.
//
//  访客视图 UI界面

import UIKit
import SnapKit
class AYuVisitorView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    func setupUI(){
        backgroundColor = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1)
        
        // 添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(regButton)
        
        // 添加约束
        feedImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 230, height: 36))
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.leading.equalTo(messageLabel)
        }
        
        regButton.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: 100, height: 36))
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.trailing.equalTo(messageLabel)
        }
    }
    
    // MARK: - 懒加载
    // 圆圈
    private lazy var feedImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 遮挡
    private lazy var maskImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 房子
    private lazy var iconImageView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 文字
    private lazy var messageLabel :UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(14)
        lab.text = "关注一些人，回这里看看有什么惊喜"
        lab.numberOfLines = 0
        lab.textAlignment = .Center
        lab.textColor = UIColor.grayColor()
        return lab
    }()
    
    // 登陆按钮
    private lazy var loginButton : UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        return btn
    }()
    
    // 注册按钮
    private lazy var regButton : UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        return btn
    }()
    
    // MARK: - 监听按钮点击方法 并返回闭包
    
    // 声明一个闭包
    var closure :(()->())?
    
    @objc private func loginButtonClick(){
        closure?()
    }
    
    // MARK: - 提供一个外部接口
    func setupVisitorViewInfo(imgName:String?,title:String?){
        if let img = imgName,let tit = title {
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            feedImageView.hidden = true
        }else{
            // 代表首页 添加动画
            setupAnim()
        }
    }
    
    // 设置圆圈动画
    func setupAnim(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = M_PI * 2
        anim.duration = 10
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        feedImageView.layer.addAnimation(anim, forKey: nil)
    }
    
}
